package com.ruoyi.cloudcourse.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cloudcourse.domain.FeatherChapter;
import com.ruoyi.cloudcourse.service.IFeatherChapterService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

import javax.servlet.http.HttpSession;

/**
 * 总章节Controller
 * 
 * @author ruoyi
 * @date 2021-07-14
 */
@Controller
@RequestMapping("/cloudcourse/chapter")
public class FeatherChapterController extends BaseController
{
    private String prefix = "cloudcourse/chapter";

    @Autowired
    private IFeatherChapterService featherChapterService;

    @RequiresPermissions("cloudcourse:chapter:view")
    @GetMapping()
    public String chapter(Long courseId,Model model)
    {
        model.addAttribute("courseId",courseId);
        return prefix + "/chapter";
    }

    /**
     * 查询总章节树列表
     */
    @RequiresPermissions("cloudcourse:chapter:list")
    @PostMapping("/list")
    @ResponseBody
    public List<FeatherChapter> list(FeatherChapter featherChapter,HttpSession session)
    {
        session.setAttribute("courseId",featherChapter.getCourseId());
        List<FeatherChapter> list = featherChapterService.selectFeatherChapterList(featherChapter);
        return list;
    }

    /**
     * 导出总章节列表
     */
    @RequiresPermissions("cloudcourse:chapter:export")
    @Log(title = "总章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FeatherChapter featherChapter)
    {
        List<FeatherChapter> list = featherChapterService.selectFeatherChapterList(featherChapter);
        ExcelUtil<FeatherChapter> util = new ExcelUtil<FeatherChapter>(FeatherChapter.class);
        return util.exportExcel(list, "总章节数据");
    }

    /**
     * 新增总章节
     */
    @GetMapping(value = { "/add/{chapterId}", "/add/" })
    public String add(@PathVariable(value = "chapterId", required = false) Long chapterId, ModelMap mmap,HttpSession session,Model model)
    {
        if (StringUtils.isNotNull(chapterId))
        {
            mmap.put("featherChapter", featherChapterService.selectFeatherChapterById(chapterId));
        }
        model.addAttribute("courseId",(Long) session.getAttribute("courseId"));
        return prefix + "/add";
    }

    @GetMapping(value = { "/subadd" })
    public String subadd(Long chapterId, ModelMap mmap,HttpSession session,Model model)
    {
        if (StringUtils.isNotNull(chapterId))
        {
            mmap.put("featherChapter", featherChapterService.selectFeatherChapterById(chapterId));
        }
        model.addAttribute("courseId",(Long) session.getAttribute("courseId"));
        return prefix + "/subadd";
    }

    /**
     * 新增保存总章节
     */
    @RequiresPermissions("cloudcourse:chapter:add")
    @Log(title = "总章节", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FeatherChapter featherChapter,Long courseId)
    {
        System.err.println(courseId);
        return toAjax(featherChapterService.insertFeatherChapter(featherChapter));
    }

    /**
     * 修改总章节
     */
    @GetMapping("/edit/{chapterId}")
    public String edit(@PathVariable("chapterId") Long chapterId, ModelMap mmap)
    {
        FeatherChapter featherChapter = featherChapterService.selectFeatherChapterById(chapterId);
        mmap.put("featherChapter", featherChapter);
        return prefix + "/edit";
    }

    @GetMapping("/subedit")
    public String subedit(Long chapterId, ModelMap mmap)
    {
        FeatherChapter featherChapter = featherChapterService.selectFeatherChapterById(chapterId);
        mmap.put("featherChapter", featherChapter);
        return prefix + "/subedit";
    }

    /**
     * 修改保存总章节
     */
    @RequiresPermissions("cloudcourse:chapter:edit")
    @Log(title = "总章节", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FeatherChapter featherChapter)
    {
        return toAjax(featherChapterService.updateFeatherChapter(featherChapter));
    }

    /**
     * 删除
     */
    @RequiresPermissions("cloudcourse:chapter:remove")
    @Log(title = "总章节", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{chapterId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("chapterId") Long chapterId)
    {
        return toAjax(featherChapterService.deleteFeatherChapterById(chapterId));
    }

    /**
     * 选择总章节树
     */
    @GetMapping(value = { "/selectChapterTree/{chapterId}", "/selectChapterTree/" })
    public String selectChapterTree(@PathVariable(value = "chapterId", required = false) Long chapterId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(chapterId))
        {
            mmap.put("featherChapter", featherChapterService.selectFeatherChapterById(chapterId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载总章节树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = featherChapterService.selectFeatherChapterTree();
        return ztrees;
    }
}
